package com.amoyt.project.controller.admin;


import com.amoyt.project.annotations.BusinessLog;
import com.amoyt.project.annotations.ModuleLog;
import com.amoyt.project.constants.LogType;
import com.amoyt.project.domain.dto.AdminLoginDto;

import com.amoyt.project.domain.dto.insert.AdminAddDto;
import com.amoyt.project.domain.dto.query.AdminPageQuery;
import com.amoyt.project.domain.dto.update.AdminUpdateDto;
import com.amoyt.project.domain.res.Result;

import com.amoyt.project.exception.BusinessException;
import com.amoyt.project.exception.enums.BusinessExceptionEnum;
import com.amoyt.project.service.admin.AdminService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Date: 2025/8/8
 * @Time: 14:47
 * @Author: TQ_QT
 * @Description: 管理员登录
 */
@RestController
@RequestMapping("admin")
@Slf4j
@Tag(name = "管理员相关接口",description = "管理员登录接口")
@ModuleLog(module = "管理员-用户模块")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @return
     */
    @PostMapping("login")
    @Operation(summary = "管理员登录",method = "POST")
    @BusinessLog(business = "管理员登录",type = LogType.LOG_LOGIN)
    public Result login(@RequestBody AdminLoginDto adminLoginDto){
        //1.数据校验
        if(adminLoginDto.getUsername() == null || adminLoginDto.getUsername().equals("")){
            return Result.error("用户名不能为空");
        }
        if(adminLoginDto.getPassword() == null || adminLoginDto.getPassword().equals("")){
            return Result.error("密码不能为空");
        }
        log.info("管理员登录：username={}，password={}", adminLoginDto.getUsername(), adminLoginDto.getPassword());

        return adminService.login(adminLoginDto);
    }

    /**
     * 管理员登出
     * @return
     */
    @GetMapping("logout")
    @Operation(summary = "管理员登出",method = "GET")
    @BusinessLog(business = "管理员登出",type = LogType.LOG_LOGOUT)
    public Result logout(){
        return adminService.logout();
    }

    /**
     * 管理员信息
     * @return
     */
    @GetMapping("info")
    @Operation(summary = "管理员信息",method = "GET")
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin','village_admin')")
    public Result info(){
        return adminService.getAdminInfo();
    }

    /**
     * 下级管理员列表
     * @return
     */
    @PostMapping("list")
    @Operation(summary = "下级管理员列表",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result list(@RequestBody AdminPageQuery adminPageQuery){
        return adminService.listAdmins(adminPageQuery);
    }

    /**
     * 重置密码
     * @return
     */
    @PutMapping("reset/{userId}")
    @Operation(summary = "重置密码",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "重置管理员密码")
    public Result resetPassword(@PathVariable String userId){
        if(userId == null || userId.equals("")){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return adminService.resetPassword(userId);
    }

    /**
     * 删除多个管理员
     * @return
     */
    @DeleteMapping("delete/{userIds}")
    @Operation(summary = "删除管理员",method = "DELETE")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin')")
    @BusinessLog(business = "删除管理员")
    public Result delete(@PathVariable List<String> userIds){
        if(userIds == null || userIds.isEmpty()){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return adminService.delete(userIds);
    }


    /**
     * 更新信息
     * @param adminUpdateDto
     * @return
     */
    @PutMapping("update")
    @Operation(summary = "更新用户信息",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin')")
    @BusinessLog(business = "更新管理员信息")
    public Result update(@RequestBody AdminUpdateDto adminUpdateDto){
        //校验参数
        if(adminUpdateDto ==null || adminUpdateDto.getUserId() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return adminService.update(adminUpdateDto);
    }

    @PutMapping("update-own")
    @Operation(summary = "更新个人信息",method = "PUT")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "更新个人信息信息")
    public Result updateOwn(@RequestBody AdminUpdateDto adminUpdateDto){
        //校验参数
        if(adminUpdateDto ==null || adminUpdateDto.getUserId() == null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }
        return adminService.updateOwn(adminUpdateDto);
    }

    /**
     * 添加管理员
     * @param adminAddDto
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加管理员",method = "POST")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    @BusinessLog(business = "添加管理员")
    public Result add(@RequestBody AdminAddDto adminAddDto){
        //校验参数
        if(adminAddDto ==null || adminAddDto.getRoleKey() == null || adminAddDto.getUserName()== null){
            throw new BusinessException(BusinessExceptionEnum.PARAM_ERROR);
        }

        return adminService.add(adminAddDto);
    }

    @GetMapping("getLowerPermis")
    @Operation(summary = "获取下级权限",method = "GET")
    @Parameter(name = "token",description = "访问令牌",required = true,in = ParameterIn.HEADER)
    @PreAuthorize("hasAnyRole('super_admin','prov_admin','city_admin','region_admin')")
    public Result getLowerPermission(){
        return adminService.getLowerPermission();
    }
}
